<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="mini"
      :inline="true"
      v-show="showSearch"
      label-width="101px"
    >
      <el-form-item label="一级类目" prop="oneCategoryId">
        <el-select
          @change="changeOne"
          v-model="queryParams.oneCategoryId"
          placeholder="请选择"
        >
          <el-option label="请选择" :value="null"></el-option>
          <el-option
            v-for="item in categoryList1"
            :key="item.id"
            :label="item.categoryName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级类目" prop="twoCategoryId">
        <el-select
          @change="changeTwo"
          v-model="queryParams.twoCategoryId"
          placeholder="请选择"
        >
          <el-option label="请选择" :value="null"></el-option>
          <el-option
            v-for="item in categoryList2"
            :key="item.id"
            :label="item.categoryName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级类目" prop="threeCategoryId">
        <el-select v-model="queryParams.threeCategoryId" placeholder="请选择">
          <el-option label="请选择" :value="null"></el-option>
          <el-option
            v-for="item in categoryList3"
            :key="item.id"
            :label="item.categoryName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label-width="40" label="ID" prop="id">
        <el-input
          type="number"
          v-model="queryParams.id"
          placeholder="ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="三级类目名称"
        label-width="100px"
        prop="threeCategoryName"
      >
        <el-input
          v-model.trim="queryParams.threeCategoryName"
          placeholder="三级类目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:info:add']"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="infoList"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ backgroundColor: '#F7F8FA' }"
      height="380"
    >
      <el-table-column
        fixed="left"
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column width="55" label="ID" align="center" prop="id" />
      <el-table-column
        label="一级类目"
        align="center"
        prop="oneCategoryName"
        show-overflow-tooltip
      />
      <el-table-column
        label="二级类目"
        align="center"
        prop="twoCategoryName"
        show-overflow-tooltip
      />
      <el-table-column
        label="三级类目"
        align="center"
        prop="threeCategoryName"
        show-overflow-tooltip
      />
      <el-table-column label="最后编辑时间" align="center" prop="updateTime" show-overflow-tooltip/>
      <el-table-column
        width="160px"
        fixed="right"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:info:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:info:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品-类目123级关系对话框 -->
    <el-dialog
      class="dialog-box"
      :title="title"
      :visible.sync="open"
      width="850px"
      append-to-body
      :before-close="cancel"
      :close-on-click-modal="false"
    >
      <el-tabs v-model="activeName">
        <el-tab-pane label="三级类目" name="three">
          <div class="edit-form">
            <div class="form-label">
              <span class="form-red">*</span> 所属类目
            </div>
            <el-form
              class="form-value"
              ref="form-three"
              :model="form.three"
              :rules="rules.three"
              label-position="top"
              label-width="80px"
            >
              <el-form-item label="一级类目" prop="oneCategoryName">
                <el-select
                  @change="dialogSelectChange(2)"
                  v-model="form.three.oneCategoryName"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in categoryList1"
                    :key="item.id"
                    :label="item.categoryName"
                    :value="item.categoryName"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="二级类目" prop="twoCategoryName">
                <el-select
                  @change="dialogSelectChange(3)"
                  v-model="form.three.twoCategoryName"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in categoryList2"
                    :key="item.id"
                    :label="item.categoryName"
                    :value="item.categoryName"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="三级类目" prop="threeCategoryName">
                <el-input
                  v-model="form.three.threeCategoryName"
                  placeholder="请填写类目名称"
                />
              </el-form-item>
            </el-form>
          </div>
        </el-tab-pane>
        <el-tab-pane label="二级类目" name="two">
          <div class="edit-form">
            <div class="form-label">
              <span class="form-red">*</span> 所属类目
            </div>
            <el-form
              ref="form-two"
              :model="form.two"
              :rules="rules.two"
              class="form-value"
              label-position="top"
              label-width="80px"
            >
              <el-form-item label="一级类目" prop="oneCategoryName">
                <el-select
                  @change="dialogSelectChange(2)"
                  v-model="form.two.oneCategoryName"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in categoryList1"
                    :key="item.id"
                    :label="item.categoryName"
                    :value="item.categoryName"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="二级类目" prop="twoCategoryName">
                <el-input
                  v-model="form.two.twoCategoryName"
                  placeholder="请填写类目名称"
                />
              </el-form-item>
              <el-form-item label="三级类目" prop="threeCategoryName">
                <el-input
                  v-model="form.two.threeCategoryName"
                  placeholder="请填写类目名称"
                />
              </el-form-item>
            </el-form>
          </div>
        </el-tab-pane>
        <el-tab-pane label="一级类目" name="one">
          <div class="edit-form">
            <div class="form-label">
              <span class="form-red">*</span> 所属类目
            </div>
            <el-form
              ref="form-one"
              :model="form.one"
              :rules="rules.one"
              class="form-value"
              label-position="top"
              label-width="80px"
            >
              <el-form-item label="一级类目" prop="oneCategoryName">
                <el-input
                  v-model="form.one.oneCategoryName"
                  placeholder="请填写类目名称"
                />
              </el-form-item>
              <el-form-item label="二级类目" prop="twoCategoryName">
                <el-input
                  v-model="form.one.twoCategoryName"
                  placeholder="请填写类目名称"
                />
              </el-form-item>
              <el-form-item label="三级类目" prop="threeCategoryName">
                <el-input
                  v-model="form.one.threeCategoryName"
                  placeholder="请填写类目名称"
                />
              </el-form-item>
            </el-form>
          </div>
        </el-tab-pane>
      </el-tabs>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  updateInfo,
  listOnInfo,
} from "@/api/service/info";

export default {
  name: "Info",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品-类目123级关系表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        oneCategoryId: null,
        twoCategoryId: null,
        threeCategoryId: null,
        threeCategoryName: null,
      },
      // 表单参数
      form: {
        one: {},
        two: {},
        three: {},
      },
      // 表单校验
      rules: {
        one: {
          oneCategoryId: [
            { required: true, message: "请选择", trigger: "blur" },
          ],
          twoCategoryId: [
            { required: true, message: "请选择", trigger: "blur" },
          ],
          threeCategoryId: [
            { required: true, message: "请选择", trigger: "blur" },
          ],
          oneCategoryName: [
            {
              required: true,
              message: "一级类目名称不能为空",
              trigger: "blur",
            },
          ],
          twoCategoryName: [
            {
              required: true,
              message: "二级类目名称不能为空",
              trigger: "blur",
            },
          ],
          threeCategoryName: [
            {
              required: true,
              message: "三级类目名称不能为空",
              trigger: "blur",
            },
          ],
        },
        two: {
          oneCategoryId: [
            { required: true, message: "请选择", trigger: "blur" },
          ],
          twoCategoryId: [
            { required: true, message: "请选择", trigger: "blur" },
          ],
          threeCategoryId: [
            { required: true, message: "请选择", trigger: "blur" },
          ],
          oneCategoryName: [
            {
              required: true,
              message: "一级类目名称不能为空",
              trigger: "blur",
            },
          ],
          twoCategoryName: [
            {
              required: true,
              message: "二级类目名称不能为空",
              trigger: "blur",
            },
          ],
          threeCategoryName: [
            {
              required: true,
              message: "三级类目名称不能为空",
              trigger: "blur",
            },
          ],
        },
        three: {
          oneCategoryId: [
            { required: true, message: "请选择", trigger: "blur" },
          ],
          twoCategoryId: [
            { required: true, message: "请选择", trigger: "blur" },
          ],
          threeCategoryId: [
            { required: true, message: "请选择", trigger: "blur" },
          ],
          oneCategoryName: [
            {
              required: true,
              message: "一级类目名称不能为空",
              trigger: "blur",
            },
          ],
          twoCategoryName: [
            {
              required: true,
              message: "二级类目名称不能为空",
              trigger: "blur",
            },
          ],
          threeCategoryName: [
            {
              required: true,
              message: "三级类目名称不能为空",
              trigger: "blur",
            },
          ],
        },
      },

      activeName: "three",
      categoryList1: [], // 分类一
      categoryList2: [], // 分类二
      categoryList3: [], // 分类三
    };
  },
  created() {
    this.getList();
    this.getListOnInfo();
  },
  methods: {
    dialogSelectChange(type) {
      const parmasEmnu = {
        2: {
          fn: () => {
            this.categoryList2 = [];
            this.categoryList3 = [];
            this.form[this.activeName].twoCategoryName = "";
            this.form[this.activeName].threeCategoryName = "";
          },
          oneCategoryId: this["categoryList1"].find(
            (item) =>
              item.categoryName == this.form[this.activeName].oneCategoryName
          )?.id,
        },
        3: {
          fn: () => {
            this.categoryList3 = [];
            this.form[this.activeName].threeCategoryName = "";
          },
          oneCategoryId: this["categoryList1"].find(
            (item) =>
              item.categoryName == this.form[this.activeName].oneCategoryName
          )?.id,
          twoCategoryId: this["categoryList2"].find(
            (item) =>
              item.categoryName == this.form[this.activeName].twoCategoryName
          )?.id,
        },
      };
      if (Object.hasOwnProperty.call(parmasEmnu, type)) {
        parmasEmnu[type].fn();
        delete parmasEmnu[type].fn;
        listOnInfo({
          type,
          ...parmasEmnu[type],
        }).then((response) => {
          this["categoryList" + type] = response.data;
          console.log(this["categoryList" + type]);
        });
      }
    },
    changeOne() {
      this.queryParams.twoCategoryId = null;
      this.queryParams.threeCategoryId = null;
      this.categoryList2 = [];
      this.categoryList3 = [];
      if (!this.queryParams.oneCategoryId) {
        return;
      }
      listOnInfo({
        type: 2,
        oneCategoryId: this.queryParams.oneCategoryId,
      }).then((response) => {
        this.categoryList2 = response.data;
      });
    },
    changeTwo() {
      this.queryParams.threeCategoryId = null;
      this.categoryList3 = [];
      if (!this.queryParams.twoCategoryId) {
        return;
      }
      listOnInfo({
        type: 3,
        oneCategoryId: this.queryParams.oneCategoryId,
        twoCategoryId: this.queryParams.twoCategoryId,
      }).then((response) => {
        this.categoryList3 = response.data;
      });
    },
    // 查询 一级 二级 三级 目录数据
    getListOnInfo() {
      listOnInfo({ type: 1 }).then((res) => {
        this.categoryList1 = res.data;
      });
      // listOnInfo({ type: 2 }).then( res => {
      //     this.categoryList2 = res.data
      // })
      // listOnInfo({ type: 3 }).then( res => {
      //     this.categoryList3 = res.data
      // })
    },
    /** 查询产品-类目123级关系列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then((res) => {
        const response = res.data;
        this.infoList = response.records;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form.one = {
        id: null,
        oneCategoryId: null,
        oneCategoryName: null,
        threeCategoryId: null,
        threeCategoryName: null,
        twoCategoryId: null,
        twoCategoryName: null,
        updateTime: null,
      };
      this.form.two = {
        id: null,
        oneCategoryId: null,
        oneCategoryName: null,
        threeCategoryId: null,
        threeCategoryName: null,
        twoCategoryId: null,
        twoCategoryName: null,
        updateTime: null,
      };
      this.form.three = {
        id: null,
        oneCategoryId: null,
        oneCategoryName: null,
        threeCategoryId: null,
        threeCategoryName: null,
        twoCategoryId: null,
        twoCategoryName: null,
        updateTime: null,
      };

      this.activeName = "three";
      this.resetForm("form");
      this.$refs["form-three"]?.resetFields();
      this.$refs["form-two"]?.resetFields();
      this.$refs["form-one"]?.resetFields();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.categoryList2 = [];
      this.categoryList3 = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增类目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getInfo(id).then((response) => {
        this.form.one = response.data;
        this.form.two = response.data;
        this.form.three = response.data;
        this.open = true;
        this.title = "修改类目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form-" + this.activeName].validate((valid) => {
        if (valid) {
          if (this.form[this.activeName].id != null) {
            updateInfo(this.form[this.activeName]).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.resetQuery();
            });
          } else {
            addInfo(this.form[this.activeName]).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.resetQuery();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除产品-类目123级关系编号为"' + ids + '"的数据项？')
        .then(function () {
          return delInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/info/export",
        {
          ...this.queryParams,
        },
        `info_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>


<style lang="scss" scoped>
.app-container{
  overflow: hidden;
}
::v-deep .el-table{
  .el-table__fixed-body-wrapper{
    top: 44px !important;
  }
}

.edit-form {
  display: flex;
  .form-label {
    font-size: 14px;
    font-weight: bold;
    line-height: 36px;
    padding-right: 30px;
    .form-red {
      color: #d23c26;
    }
  }
  .form-value {
    display: flex;
    gap: 30px;
  }
}

.dialog-box ::v-deep .el-input {
  width: 218px;
}
</style>